package com.himiotech.core.project.web.system;

import com.himiotech.core.framework.helper.anno.Log;
import com.himiotech.core.framework.helper.constant.UserConstants;
import com.himiotech.core.framework.helper.enums.BusinessType;
import com.himiotech.core.framework.helper.model.SimpleDataTree;
import com.himiotech.core.project.model.entity.Dept;
import com.himiotech.core.project.model.entity.role.Role;
import com.himiotech.core.project.service.IDeptService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 *
 * @author ruoyi
 * @author wuwenbin
 */
@Controller
@RequestMapping("/system/dept")
public class DeptController extends HimioBaseController {

    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("system:dept:view")
    @GetMapping
    public String dept() {
        return "system/dept/list";
    }

    @RequiresPermissions("system:dept:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Dept> list(Dept dept) {
        return deptService.selectDeptList(dept);
    }


    /**
     * 新增部门
     */
    @Log(title = "部门管理 - 新增", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dept:add")
    @PostMapping("/add")
    @ResponseBody
    public HimioResult addSave(@Validated Dept dept) {
        if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
            return writeJsonErr("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        return writeJson(deptService.insertDept(dept));
    }


    /**
     * 修改部门
     */
    @Log(title = "部门管理 - 修改", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dept:edit")
    @PostMapping("/edit")
    @ResponseBody
    public HimioResult editSave(@Validated Dept dept) {
        if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
            return writeJsonErr("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        } else if (dept.getParentId().equals(dept.getDeptId())) {
            return writeJsonErr("修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
                && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0) {
            return writeJsonErr("该部门包含未停用的子部门！");
        }
        return writeJson(deptService.updateDept(dept));
    }

    /**
     * 删除
     */
    @Log(title = "部门管理 - 删除", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dept:remove")
    @GetMapping("/remove/{deptId}")
    @ResponseBody
    public HimioResult remove(@PathVariable("deptId") Long deptId) {
        if (deptService.selectDeptCount(deptId) > 0) {
            return writeJsonErr("存在下级部门,不允许删除");
        }
        if (deptService.checkDeptExistUser(deptId)) {
            return writeJsonErr("部门存在用户,不允许删除");
        }
        return writeJson(deptService.deleteDeptById(deptId));
    }


    /**
     * 加载部门列表树
     */
    @GetMapping("/roleTreeData")
    @ResponseBody
    public List<SimpleDataTree<Dept>> roleTreeData() {
        return deptService.selectDeptTree(new Dept());
    }

    /**
     * 加载角色部门（数据权限）列表树
     */
    @GetMapping("/roleDeptTreeData")
    @ResponseBody
    public List<SimpleDataTree<Dept>> deptTreeData(Role role) {
        return deptService.roleDeptTreeData(role);
    }

}
